package com.example.pkq;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;

//计算器
public class Calculator extends AppCompatActivity {

    private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
    private Button clears, btnAdd, btnLess, btnBy, btnExcept, btnPoint, btnEqual, tuige;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        //找到按钮
        editText = findViewById(R.id.c_num);
        clears = findViewById(R.id.clear);
        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btnAdd = findViewById(R.id.btnAdd);
        btnLess = findViewById(R.id.btnLess);
        btnBy = findViewById(R.id.btnBy);
        btnExcept = findViewById(R.id.btnExcept);
        btnPoint = findViewById(R.id.btnPoint);
        btnEqual = findViewById(R.id.btnEqual);
        tuige = findViewById(R.id.tuige);
        //按钮点击事件
        OnClick on = new OnClick();
        btn0.setOnClickListener(on);
        btn1.setOnClickListener(on);
        btn2.setOnClickListener(on);
        btn3.setOnClickListener(on);
        btn4.setOnClickListener(on);
        btn5.setOnClickListener(on);
        btn6.setOnClickListener(on);
        btn7.setOnClickListener(on);
        btn8.setOnClickListener(on);
        btn9.setOnClickListener(on);
        btnAdd.setOnClickListener(on);
        btnLess.setOnClickListener(on);
        btnBy.setOnClickListener(on);
        btnExcept.setOnClickListener(on);
        btnPoint.setOnClickListener(on);
        btnEqual.setOnClickListener(on);
        clears.setOnClickListener(on);
        tuige.setOnClickListener(on);
    }

    private class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            String input = editText.getText().toString();
            switch (view.getId()) {
                case R.id.btn0:
                case R.id.btn1:
                case R.id.btn2:
                case R.id.btn3:
                case R.id.btn4:
                case R.id.btn5:
                case R.id.btn6:
                case R.id.btn7:
                case R.id.btn8:
                case R.id.btn9:
                case R.id.btnPoint:
                    editText.setText(input + ((Button) view).getText());
                    break;
                case R.id.btnAdd:
                case R.id.btnLess:
                case R.id.btnBy:
                case R.id.btnExcept:
                    editText.setText(input + "" + ((Button) view).getText() + "");
                    break;
                case R.id.btnEqual:
                    double sum = getNum();
                    input = Double.toString(sum);
                    editText.setText(input);
                    break;
                case R.id.tuige:
                    String tui = input.substring(0, input.length() - 1);//退格：减去现有字符最后一格
                    editText.setText(tui);
                    break;
                case R.id.clear:
                    editText.setText("");
                    break;
            }
        }
    }

    public double getNum() {
        String input = editText.getText().toString();
        String sa = input;
        String[] str = null;
        char fu = ' ';//初始化运算符
        for (int i = 0; i < sa.length(); i++) {//判断运算符是否为
            char aa = sa.charAt(i);
            if (aa == '+') {
                str = sa.split("\\+");
                fu = '+';
            }
            if (aa == '-') {
                str = sa.split("-");
                fu = '-';
            }
            if (aa == '*') {
                str = sa.split("\\*");
                fu = '*';
            }
            if (aa == '/') {
                str = sa.split("/");
                fu = '/';
            }
        }
        double sum = jisuan(str, fu);
        return sum;
    }

    public static double jisuan(String[] shuzi, char fuhao) {
        ArrayList<Double> ar = new ArrayList<>();
        double sum = 0;
        for (int i = 0; i < shuzi.length; i++) {
            double s = Double.parseDouble(shuzi[i]);
            ar.add(s);
        }
        if (fuhao == '+') {
            sum = ar.get(0) + ar.get(1);
        }
        if (fuhao == '-') {
            sum = ar.get(0) - ar.get(1);
        }
        if (fuhao == '*') {
            sum = ar.get(0) * ar.get(1);
        }
        if (fuhao == '/') {
            sum = ar.get(0) / ar.get(1);
        }if (fuhao ==' '){
            return sum;
        }
        return sum;
    }
}
